home *** CD-ROM | disk | FTP | other *** search
/ Die Speccy' 97 / Die Speccy' 97.iso / amiga_system / the_aminet / comm / bbs / s342q07.lha / tools.c < prev    next >
C/C++ Source or Header  |  1993-06-18  |  4KB  |  196 lines

  1. /*
  2. *                tools.c
  3. *
  4. * Random functions for both Ctdl and it's utilities.
  5. */
  6. /*
  7. *                history
  8. *
  9. * 90Aug26 HAW  Created.
  10. */
  11. #include "ctdl.h"
  12. /*
  13. *                Contents
  14. *
  15. *    NormStr()        Deletes leading trailing blanks etc.
  16. *    PrintPretty()        Print numbers prettily.
  17. *    normId()        Normalizes a node id.
  18. *    hash()            Hashes a string to an integer.
  19. *    CleanEnd()        Clears trailing blanks off.
  20. *    lbyte()            Finds the 0 byte of a string.
  21. */
  22. void  Do_Stack_Check(void);
  23. /*
  24. * NormStr()
  25. *
  26. * This function Deletes leading trailing blanks etc.
  27. */
  28. void NormStr(char *s)
  29.   {
  30.   char *save,*pc;
  31.   Do_Stack_Check();
  32.   pc = save = s;
  33.   /* find end of string and remove control characters   */
  34.   while (*pc)
  35.     {
  36.     if (*pc < ' ')   *pc = ' ';   /* zap tabs etc... */
  37.     pc++;
  38.     
  39.     };
  40.   pc--;                              /* no trailing spaces: */
  41.   while (pc>s  && *pc == ' ') pc--;
  42.   pc++;
  43.   *pc = '\0';                        /* no leading spaces: */
  44.   pc = s;
  45.   while( *pc == ' ') pc++;
  46.   if( pc != s )
  47.     {
  48.     while(*pc)*s++ = *pc++;
  49.     *s = '\0';
  50.     
  51.     };
  52.   s = save;                          /* no double blanks */
  53.   for (;  *s;)
  54.     {
  55.     if (*s == ' '   &&   *(s+1) == ' ')
  56.       {
  57.       for (pc=s;  *pc;  pc++)    *pc = *(pc+1);
  58.       
  59.       }
  60.     else s++;
  61.     
  62.     };
  63.   
  64.   }
  65. static long StartDiv = 1000000l;
  66. static char FirstFlag = TRUE;
  67. /*
  68. * PrintPretty()
  69. *
  70. * This will pretty print a long with commas.
  71. */
  72. char *PrintPretty(long s, char *result)
  73.   {
  74.   Do_Stack_Check();
  75.   if (StartDiv == 1)
  76.     {
  77.     sprintf(result, FirstFlag ? "%ld" : "%03ld", s);
  78.     FirstFlag = TRUE;
  79.     StartDiv  = 1000000l;
  80.     return result;
  81.     
  82.     }
  83.   if (s >= StartDiv)
  84.     {
  85.     sprintf(result, FirstFlag ? "%ld," : "%03ld,", s / StartDiv);
  86.     FirstFlag = FALSE;
  87.     s %= StartDiv;
  88.     StartDiv /= 1000;
  89.     PrintPretty(s, result + strlen(result));
  90.     
  91.     }
  92.   else
  93.     {
  94.     StartDiv /= 1000;
  95.     PrintPretty(s, result);
  96.     
  97.     }
  98.   return result;
  99.   
  100.   }
  101. /*
  102. * normId()
  103. *
  104. * This function normalizes a node id.
  105. ***duplicate of lib source
  106. char normId(label source, label dest)
  107.   {
  108.   int digitcount = 0;
  109.   while (!isalpha(*source) && *source)
  110.   source++;
  111.   if (!*source) return FALSE;
  112.   *dest++ = toUpper(*source++);
  113.   while (!isalpha(*source) && *source)
  114.   source++;
  115.   if (!*source) return FALSE;
  116.   *dest++ = toUpper(*source++);
  117.   while (*source)
  118.     {
  119.     if (isdigit(*source))
  120.       {
  121.       *dest++ = *source;
  122.       digitcount++;
  123.       }
  124.       source++;
  125.       }
  126.       *dest = '\0';
  127.       return (digitcount > 8);
  128.       }
  129.       Commented out NormId */
  130.       /*
  131.       * hash()
  132.       *
  133.       * This function hashes a string to an integer.
  134.       */
  135.       UNS_16 hash(char *str)
  136.         {
  137.         UNS_16  h, shift;
  138.         Do_Stack_Check();
  139.         for (h=shift=0;  *str;  shift=(shift+1)&7, str++)
  140.           {
  141.           h ^= (toUpper(*str)) << shift;
  142.           
  143.           }
  144.         return h;
  145.         
  146.         }
  147.       /*
  148.       * CleanEnd()
  149.       *
  150.       * This function cleans up a message trailer for later display via Continue or
  151.       * .EH.  Inspired by Glen Heinz (MacCitadel).
  152.       */
  153.       char *CleanEnd(char *text)
  154.         {
  155.         char *ptr;
  156.         int  wc, lc;    /* Word Count and Letter Count */
  157.         Do_Stack_Check();
  158.         if (strLen(text) == 0) return text;
  159.         ptr = lbyte(text) - 1;      /* End of text of msg */
  160.         /*
  161.         * Strip trailing whitespace.  We structure the loop this
  162.         * way to avoid any chance of accidentally accessing memory outside
  163.         * of the memory area.
  164.         */
  165.         while (ptr != text - 1)
  166.           {
  167.           if (!(*ptr == ' ' || *ptr == NEWLINE || *ptr == TAB)) break;
  168.           ptr--;
  169.           
  170.           }
  171.         ptr++;    /* point at byte following last significant character */
  172.         *ptr = 0;    /* tie it off with a NULL */
  173.         /* Now we want to find a "preferred place" */
  174.         for (wc = lc = 0, ptr--; wc < 4 && ptr > text && lc < 35; ptr--, lc++)
  175.           {
  176.           if (*ptr == ' ') wc++;
  177.           if (*ptr == NEWLINE) break;     /* can't go beyond embedded NEWLINE */
  178.           
  179.           }
  180.         if (ptr == text) return ptr;    /* if msg is empty or < 35 chars long */
  181.         /* else */       return (ptr + 1);    /* else return "favored" spot */
  182.         
  183.         }
  184.       /*
  185.       * lbyte()
  186.       *
  187.       * This function finds the 0 byte of a string, returns pointer to it...
  188.       */
  189.       char *lbyte(char *l)
  190.         {
  191.         Do_Stack_Check();
  192.         while (*l) l++;
  193.         return l;
  194.         
  195.         }
  196.